import { Route, Routes, HashRouter } from 'react-router-dom';
import React, { useState } from 'react';
import { ThemeProvider } from 'styled-components';
import { getTheme } from '../styles/theme';
import { ConfigProvider, theme as antdTheme } from "antd";
import { StyleProvider } from '@ant-design/cssinjs';
import AuthProvider from '../components/auth/auth-provider';
import { RequireAuth } from '../components/auth/require-auth';
import { EmptyLayout, MainLayout } from '../layout';
import { LoginPage, WxLoginPage, SettingsPage, WxxQRCodeLoginPage, WxxLoginHomePage, ChatPage } from '../pages';
import { Theme } from '../styles/types';
import { GlobalStyle } from '../styles/global-style';
import { MGApplicationProps } from '../api';
import { ApplicationViewer } from '../components';
import '@/i18n/index';

import 'tailwindcss/tailwind.css';
import '../themes/light.css';
import '../themes/dark.css';
import { ProviderContextProvider } from '@/context/provider-context';
import { EventEmitterContextProvider } from '@/context/event-emitter';
import { ModalContextProvider } from '@/context/modal-context';
import ApplicationAgentPage from '@/pages/application/agent';

export const MGApplication: React.FC<MGApplicationProps> = (props: MGApplicationProps) => {

    const [theme, _] = useState<Theme>(Theme.Light);
    //
    return (
        <HashRouter>
            <ThemeProvider theme={getTheme(theme)}>
                <GlobalStyle />
                <ConfigProvider theme={{ algorithm: [antdTheme.defaultAlgorithm, antdTheme.compactAlgorithm] }}>
                    <StyleProvider hashPriority="high">
                        <AuthProvider>
                            <EventEmitterContextProvider>
                                <ProviderContextProvider>
                                    <ModalContextProvider>
                                        <Routes>
                                            <Route element={<RequireAuth><MainLayout  {...props} /></RequireAuth>}>
                                                {/* 需要登录 */}
                                                {
                                                    props.pages && props.pages.filter((item, index) => !item.layoutHiden).map((item, index) => <Route key={index} {...item} />)
                                                }
                                                <Route path='/_chat' element={<ApplicationAgentPage application={{
                                                    identifier: '',
                                                    name: '',
                                                    main: ''
                                                }}></ApplicationAgentPage>} />
                                                <Route path='/_settings' element={<SettingsPage />} />
                                            </Route>
                                            <Route element={<RequireAuth><EmptyLayout  {...props} /></RequireAuth>}>
                                                {/* 需要登录 */}
                                                {
                                                    props.pages && props.pages.filter((item, index) => !!item.layoutHiden).map((item, index) => <Route key={index} {...item} />)
                                                }
                                            </Route>
                                            {/* 登录 */}
                                            <Route path='/_login' element={<WxxLoginHomePage />} />
                                            <Route path='/login' element={<LoginPage />} />
                                            <Route path='/tklogin' element={<WxLoginPage />} />
                                            <Route path='/wxxlogin' element={<WxxQRCodeLoginPage />} />
                                        </Routes>
                                    </ModalContextProvider>
                                </ProviderContextProvider>
                            </EventEmitterContextProvider>
                        </AuthProvider>
                    </StyleProvider>
                </ConfigProvider>
            </ThemeProvider>
        </HashRouter>
    )
};

export default MGApplication; 
